$(function () {
    $("#PersonModule").addClass("active");

    $('#table1 tr').activeTable(
        function () {
            isSelectedUser = true;
            activeButton();
        },
        function () {
            isSelectedUser = false;
            activeButton();
        });

    $('#table2 tr').activeTable(
        function () {
            isSelectedRole = true;
            activeButton();

            getAllotUsers();
        },
        function () {
            isSelectedRole = false;
        }, true);

    $('#table3 tr').activeTable(
        function () {
            isSelectedAllotUser = true;
            activeButton();
        },
        function () {
            isSelectedAllotUser = false;
            activeButton();
        });

    $("#table1").trCheckboxToCenter();
    $("#table2").trCheckboxToCenter();
    $("#table3").trCheckboxToCenter();

    /**
     * 分配角色/撤销角色
     */
    $("#userNavigation").find("button").each(function (i) {
        $(this).click(function () {
            if (i == 0) {
                $("#table1").allotUser($(this), false);
            } else if (i == 1) {
                $("#table3").allotUser($(this), true);
            }
        });
    });

    /**
     * 为用户分配角色
     * @param btn 分配角色的按钮
     * @param isRemove 标识分配/撤销角色
     */
    $.fn.allotUser = function (btn, isRemove) {
        var users = $(this).find(".warning");
        var role = $('#table2 .warning');
        if (users.length > 0) {
            var ids = [];
            ids[0] = "int";
            users.each(function (i) {
                ids[++i] = $(this).find("td:last").html();
            });

            var data =
                "ids=" + arrayToJson(ids) + "&" +
                "value=" + role.find("td:last").prev().html() + "&" +
                "isRemove=" + isRemove;
            iAjaxPost(btn.attr("href"), data, function () {
                window.location.href = btn.attr("href") + "?roleId=" + role.find("td:last").html();
            }, btn, $("#tablePanel"));
        }
    };
});
var isSelectedUser = false;
var isSelectedRole = false;
var isSelectedAllotUser = false;
var tr = "<tr><td><input type='checkbox'/></td><td>{0}</td><td>{1}</td><td>{2}</td><td style='display: none;'>{3}</td></tr>";

/**
 * 根据选择的角色获取用户
 */
//tr:not(first) tr:gt(0)
function getAllotUsers(){
    var data = "value=" + $('#table2 .warning').find("td:last").prev().html();
    if(data != 'value=undefined'){
        iAjaxPost($("#table2").attr("source"), data, function (msg) {
            if (msg != null) {
                if (msg.data != null && msg.data.length > 0) {
                    appendUser(JSON.parse(msg.data));
                }
            }
        }, $("#table2"), $("#tablePanel"));
    }
}

/**
 * 呈现ajax请求来的数据
 * @param jsonArray
 */
function appendUser(jsonArray) {
    $("#table3-body").empty();
    $(jsonArray).each(function (i) {
        $("#table3-body").append(String.format(tr, i + 1, this.userLoginName, this.userName, this.userId));
    });

    $('#table3 tr').activeTable(
        function () {
            isSelectedAllotUser = true;
            activeButton();
        },
        function () {
            isSelectedAllotUser = false;
            activeButton();
        });
}

/**
 * 激活分配到角色的button
 */
function activeButton() {
    if (isSelectedRole && isSelectedUser) {
        $("#userNavigation button:eq(0)").removeAttr("disabled");
    }
    else {
        $("#userNavigation button:eq(0)").attr("disabled", "disabled");
    }

    if (isSelectedRole && isSelectedAllotUser) {
        $("#userNavigation button:eq(1)").removeAttr("disabled");
    }
    else {
        $("#userNavigation button:eq(1)").attr("disabled", "disabled");
    }
}